﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using System.Data.OleDb;
using System.Data;
using System.IO;

namespace OA.Controllers
{
    public class EmailController : Controller
    {
        //
        // GET: /Email/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult SendEmail()
        {
            Model.Email sender = new Email();
            EmailManager SendManager = new EmailManager();
            sender= SendManager.GetByID(1);

            return View(sender);
        }

        //从excel导入收件人
        public string TransferData(string excelFile, string sheetName)
        {
            DataSet ds = new DataSet();
            string excel = "";
            try
            {
                try
                {
                    string strConn = "Provider=Microsoft.ACE.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended properties=\"Excel 12.0;Imex=1;HDR=Yes;\"";
                    OleDbConnection conn = new OleDbConnection(strConn);
                    conn.Open();
                    string strExcel = "";
                    OleDbDataAdapter myCommand = null;
                    strExcel = string.Format("select * from [{0}$]", sheetName);
                    myCommand = new OleDbDataAdapter(strExcel, strConn);
                    myCommand.Fill(ds, sheetName);
                    bool bl = true;

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        excel += ds.Tables[0].Rows[i][0].ToString() + "\n";
                    }
                    conn.Close();
                }
                catch
                {
                    string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + excelFile + ";" + "Extended properties=\"Excel 12.0;Imex=1;HDR=Yes;\"";
                    OleDbConnection conn = new OleDbConnection(strConn);
                    conn.Open();
                    string strExcel = "";
                    OleDbDataAdapter myCommand = null;
                    strExcel = string.Format("select * from [{0}$]", sheetName);
                    myCommand = new OleDbDataAdapter(strExcel, strConn);
                    myCommand.Fill(ds, sheetName);
                    bool bl = true;
                    //如果目标表不存在则创建   
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        excel += ds.Tables[0].Rows[i][0].ToString() + "\n";

                    }
                    conn.Close();
                }
            }
            catch
            {
                string msg="发生错误了，请确保excel格式内容正确";
                return msg;
            }
            
            return excel;
        }
        //上传excel文件
        public ActionResult Upload()
        {
            HttpFileCollection hfc=System.Web.HttpContext.Current.Request.Files;
            string physicalPath= "";
            if(hfc.Count>0)
            {
               string StrEx= Path.GetExtension(hfc[0].FileName).ToLower();
               if (StrEx == ".xls" || StrEx==".xlsx")
               {
                   string path = "~/Content/SelfAdd/" + Path.GetFileName(hfc[0].FileName);
                   physicalPath = Server.MapPath(path);
                   hfc[0].SaveAs(physicalPath);
               }
               else
               {
                   return Content("excel格式不正确");
               }
            }

            string data = TransferData(physicalPath, "sheet1");
            string delPath = Server.MapPath("~/Content/SelfAdd/");
            System.IO.File.Delete(physicalPath);
            return Content(data);
        }
    }
}
